(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.fxRate.createVerifyController', editController);
	editController.$inject = [ '$scope', '$q', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'fxRateService' ];
	function editController($scope, $q, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, fxRateService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.fxRate = {};
		ctrl.fxRateOld = {};
		ctrl.fxRateHistoryLookup = function(){
			fxRateService.fxRateHistoryLookup(ctrl.selectedRows);
		};
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.general.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.fxRate.fxRateList';
			ctrl.innerGridOptions.multiSelect = false;
			
			fxRateService.initOptions().then(function(result){
				return $q(function(resolved, rejected) {
					ctrl.currencyOptions = result.currencyOptions;
					ctrl.baseCcy = result.baseCcy;
					resolved();
				});
			}).then(function(){
				authService.createVerify(params).then(function(response) {
					ctrl.fxRate = response.data.model || {};
					ctrl.fxRate.taskId = response.data.task.taskId;
					ctrl.fxRate.fxRateList = ctrl.fxRate.fxRateList || [];
				});
			});
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {		
					'ok' : function(comments) {
						ctrl.fxRate.comments = comments;
						authService.createVerifyReject(ctrl.fxRate).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.fxRate.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.fxRate).then(approveSuccessful, rejectFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.fxrate.list',
					'state' : 'cda.fxRate.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.fxrate.list',
					'state' : 'cda.fxRate.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);